<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sampleDate">
              <j-date placeholder="请选择日期" v-model="model.sampleDate"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="餐次" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="mealsType">
              <j-dict-select-tag type="list" v-model="model.mealsType" dictCode="meals_type" placeholder="请选择餐次" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="用餐方式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="mealsMethods">
              <a-input v-model="model.mealsMethods" :maxLength="10" placeholder="请输入用餐方式"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="用餐时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="mealsTime">
              <j-date placeholder="请选择用餐时间" :showTime="true" :dateFormat="'YYYY-MM-DD HH:mm:ss'" v-model="model.mealsTime"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="留样时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sampleTime">
              <j-date placeholder="请选择留样时间" :showTime="true" :dateFormat="'YYYY-MM-DD HH:mm:ss'" v-model="model.sampleTime"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="菜品重量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dishesNum">
              <a-input v-model.number="model.dishesNum" suffix="g" @blur="bulrDishesNum" placeholder="请输入菜品数量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="食品名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="foodName">
              <a-input v-model="model.foodName" :maxLength="60" placeholder="请输入食品名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="留样结束时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sampleRetentionTime">
              <j-date placeholder="请选择留样结束时间" :showTime="true" :dateFormat="'YYYY-MM-DD HH:mm:ss'" v-model="model.sampleRetentionTime"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="留样人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sampleUser">
              <a-select  v-model="model.sampleUser" placeholder="请选择留样人">
                <a-select-option v-for="item in userList" :key="item.id" :value="item.id">{{ item.realname }}</a-select-option>
              </a-select>
              <!-- <j-dict-select-tag placeholder="请选择留样人" v-model="model.sampleUser" dictCode="sys_user,realname,id,del_flag=0 and user_identity in (3,5)"/> -->
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="处理人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="processors">
              <a-select  v-model="model.processors" placeholder="请选择处理人">
                <a-select-option v-for="item in userList" :key="item.id" :value="item.id">{{ item.realname }}</a-select-option>
              </a-select>
              <!-- <j-dict-select-tag placeholder="请选择处理人" v-model="model.processors" dictCode="sys_user,realname,id,del_flag=0 and user_identity in ('3','5')"/> -->
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="监督人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="supervisor">
              <a-select  v-model="model.supervisor" placeholder="请选择监督人">
                <a-select-option v-for="item in userList" :key="item.id" :value="item.id">{{ item.realname }}</a-select-option>
              </a-select>
              <!-- <j-dict-select-tag placeholder="请选择监督人" v-model="model.supervisor" dictCode="sys_user,realname,id,del_flag=0 and user_identity in (3,5)"/> -->
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="处理时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="processingTime">
              <j-date placeholder="请选择处理时间" :showTime="true" :dateFormat="'YYYY-MM-DD HH:mm:ss'" v-model="model.processingTime"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'SkSampleRecordForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          sampleDate: [{ required: true, message: '请选择日期！' }],
          dishesNum: [{ required: true, message: '请输入菜品重量！' }],
          mealsType: [{ required: true, message: '请选择餐次！' }],
          sampleTime: [{ required: true, message: '请选择留样时间！' }],
          mealsTime: [{ required: true, message: '请选择用餐时间！' }],
          foodName: [{ required: true, message: '请输入食品名称！' }],
          sampleRetentionTime: [{ required: true, message: '请选择留样结束时间！' }],
          sampleUser: [{ required: true, message: '请选择留样人！' }],
          supervisor: [{ required: true, message: '请选择监督人！' }],
        },
        url: {
          add: "/canteen/skSampleRecord/add",
          edit: "/canteen/skSampleRecord/edit",
          queryById: "/canteen/skSampleRecord/queryById"
        },
        userList:[]
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
      this.getUserList();
    },
    methods: {
      getUserList(){
        const url = "/sys/user/list";
        const params = {
          userIdentitys:"3,5",
          pageNo:1,
          pageSize:500
        }
        getAction(url,params).then((res)=>{
          if(res.success){
            this.userList = res.result.records
          } else {
            this.message.warning(res.message);
          }
        }).catch((error)=>{
          console.error(error);
        })
      },
      bulrDishesNum(){
        this.model.dishesNum = this.model.dishesNum < 0?0:this.model.dishesNum
      },
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            this.$emit("modelLoading",that.confirmLoading)
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              this.$emit("modelLoading",that.confirmLoading)
            })
          }
         
        })
      },
    }
  }
</script>